from flask import Blueprint,jsonify
from flask_restful import Api,Resource,marshal,reqparse
from common.model_fields.new_fields import news_fields
from models.user import *

new_bp = Blueprint('new',__name__,url_prefix='/news')

api = Api(new_bp)

class AddNews(Resource):
    def post(self):
        """添加文章"""
        parser = reqparse.RequestParser()
        parser.add_argument('user_id')
        parser.add_argument('channel_id')
        parser.add_argument('title')
        parser.add_argument('cover')
        parser.add_argument('content')
        args = parser.parse_args()
        user_id = args.get('user_id')
        channel_id = args.get('channel_id')
        title = args.get('title')
        cover = args.get('cover')
        content = args.get('content')
        if not all([user_id,channel_id,title,content]):
            return jsonify(message='数据不完整',code=401)
        channel = Channel.query.filter_by(cid=channel_id).first()
        if channel:
            if cover:
                news = News(user_id=user_id,channel_id=channel_id,title=title,cover=cover,content=content)
                db.session.add(news)
                db.session.commit()
                return jsonify(message='添加文章成功',code=200)
            else:
                news = News(user_id=user_id,channel_id=channel_id,title=title,cover=None,content=content)
                db.session.add(news)
                db.session.commit()
                return jsonify(message='添加文章成功',code=200)
        else:
            return jsonify(message='所属的频道不存在',code=500)
    def put(self):
        """修改文章"""
        parser = reqparse.RequestParser()
        parser.add_argument('nid')
        parser.add_argument('user_id')
        parser.add_argument('channel_id')
        parser.add_argument('title')
        parser.add_argument('cover')
        parser.add_argument('content')
        args = parser.parse_args()
        nid = args.get('nid')
        user_id = args.get('user_id')
        channel_id = args.get('channel_id')
        title = args.get('title')
        cover = args.get('cover')
        content = args.get('content')
        if not all([user_id,channel_id,title,cover,content]):
            return jsonify(message='信息不完整',code=401)
        news = News.query.filter_by(nid=nid)
        if news:
            news.update({'user_id':user_id,'channel_id':channel_id,'title':title,'cover':cover,'content':content})
            db.session.commit()
            return jsonify(message='修改成功并且发布成功',code=200)
        else:
            return jsonify(message='文章不存在',code=500)
    def delete(self):
        """删除文章"""
        parser = reqparse.RequestParser()
        parser.add_argument('nid')
        args = parser.parse_args()
        nid = args.get('nid')
        news = News.query.filter_by(nid=nid).first()
        if news:
            News.query.filter_by(nid=nid).delete()
            return jsonify(message='删除成功',code=200)
        else:
            return jsonify(message='文章不存在',code=500)
    def get(self):
        """查询文章"""
        news = News.query.all()
        if news:
            news = marshal(news,news_fields)
            return news










api.add_resource(AddNews,'/new')
